<!DOCTYPE html>

<html>
<head>
  <title>Function</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="Array.coffee.html">
                Array.coffee.md
              </a>
            
              
              <a class="source" href="BareObject.coffee.html">
                BareObject.coffee.md
              </a>
            
              
              <a class="source" href="Boolean.coffee.html">
                Boolean.coffee.md
              </a>
            
              
              <a class="source" href="Function.coffee.html">
                Function.coffee.md
              </a>
            
              
              <a class="source" href="Math.coffee.html">
                Math.coffee.md
              </a>
            
              
              <a class="source" href="Maybe.coffee.html">
                Maybe.coffee.md
              </a>
            
              
              <a class="source" href="Nullable.coffee.html">
                Nullable.coffee.md
              </a>
            
              
              <a class="source" href="Number.coffee.html">
                Number.coffee.md
              </a>
            
              
              <a class="source" href="Object.coffee.html">
                Object.coffee.md
              </a>
            
              
              <a class="source" href="Promise.coffee.html">
                Promise.coffee.md
              </a>
            
              
              <a class="source" href="String.coffee.html">
                String.coffee.md
              </a>
            
              
              <a class="source" href="Type.coffee.html">
                Type.coffee.md
              </a>
            
              
              <a class="source" href="header.coffee.html">
                header.coffee.md
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h1 id="function">Function</h1>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">extend</span> = <span class="hljs-params">(properties)</span> -&gt;</span> Object.extend <span class="hljs-keyword">this</span>, properties</pre></div></div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <h2 id="basic-functions">Basic functions</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	Function.<span class="hljs-function"><span class="hljs-title">identity</span> = <span class="hljs-params">(x)</span> -&gt;</span> x
	
	Function.<span class="hljs-function"><span class="hljs-title">constant</span> = <span class="hljs-params">(constant)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">()</span> -&gt;</span> constant
	
	Function.<span class="hljs-function"><span class="hljs-title">args</span> = <span class="hljs-params">(args...)</span> -&gt;</span> args
	
	Function.<span class="hljs-function"><span class="hljs-title">argument</span> = <span class="hljs-params">(n)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span> args[n]
	
	Function.<span class="hljs-function"><span class="hljs-title">map</span> = <span class="hljs-params">(mapping)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(key)</span> -&gt;</span> mapping[key]
	
	Function.<span class="hljs-function"><span class="hljs-title">overload</span> = <span class="hljs-params">(variants=[])</span> -&gt;</span>
		<span class="hljs-function"><span class="hljs-title">fn</span> = <span class="hljs-params">(args...)</span> -&gt;</span>
			<span class="hljs-keyword">for</span> variant <span class="hljs-keyword">in</span> variants
				<span class="hljs-keyword">return</span> variant.apply(<span class="hljs-keyword">this</span>,args) <span class="hljs-keyword">if</span> variant.matches args...
			<span class="hljs-keyword">return</span> <span class="hljs-literal">undefined</span>
		fn.extend <span class="hljs-attribute">extend</span>: <span class="hljs-function"><span class="hljs-params">(variants2=[])</span> -&gt;</span>
			Function.overload [variants2...,variants...]
			
	Function.<span class="hljs-function"><span class="hljs-title">match</span> = <span class="hljs-params">(variants=[])</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(value)</span> -&gt;</span>
		<span class="hljs-keyword">for</span> variant <span class="hljs-keyword">in</span> variants
			<span class="hljs-keyword">return</span> variant.apply(value,value) <span class="hljs-keyword">if</span> variant.matches value
		<span class="hljs-keyword">return</span> <span class="hljs-literal">undefined</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <h2 id="function-composition">Function composition</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">then</span> = <span class="hljs-params">(fn2)</span> -&gt;</span>
		<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> PreconditionError <span class="hljs-keyword">unless</span> <span class="hljs-keyword">typeof</span> fn2 == <span class="hljs-string">'function'</span>
		fn1 = <span class="hljs-keyword">this</span>
		<span class="hljs-function"><span class="hljs-params">(first,rest...)</span> -&gt;</span>
			context = <span class="hljs-keyword">if</span> <span class="hljs-keyword">this</span> != <span class="hljs-built_in">window</span> <span class="hljs-keyword">then</span> <span class="hljs-keyword">this</span> ? first <span class="hljs-keyword">else</span> first
			val1 = fn1.call context, first, rest...
			fn2.call val1, val1 
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">but</span> = <span class="hljs-params">(fn2)</span> -&gt;</span>
		<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> PreconditionError <span class="hljs-keyword">unless</span> <span class="hljs-keyword">typeof</span> fn2 == <span class="hljs-string">'function'</span>
		fn1 = <span class="hljs-keyword">this</span>
		<span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span>
			fn1.apply <span class="hljs-keyword">this</span>, args
			fn2.apply <span class="hljs-keyword">this</span>, args
	
	Function.<span class="hljs-function"><span class="hljs-title">pipe</span> = <span class="hljs-params">(fn,fns...)</span> -&gt;</span>
		<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> PreconditionError <span class="hljs-keyword">unless</span> <span class="hljs-keyword">typeof</span> fn <span class="hljs-keyword">in</span> [<span class="hljs-string">'function'</span>,<span class="hljs-string">'undefined'</span>]
		<span class="hljs-keyword">switch</span> arguments.length
			<span class="hljs-keyword">when</span> <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> fn
			<span class="hljs-keyword">when</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> Function.identity
			<span class="hljs-keyword">else</span> fn.<span class="hljs-keyword">then</span> Function.pipe fns...
	
	Function.<span class="hljs-function"><span class="hljs-title">compose</span> = <span class="hljs-params">(fn,fns...)</span> -&gt;</span>
		<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> PreconditionError <span class="hljs-keyword">unless</span> <span class="hljs-keyword">typeof</span> fn <span class="hljs-keyword">in</span> [<span class="hljs-string">'function'</span>,<span class="hljs-string">'undefined'</span>]
		<span class="hljs-keyword">switch</span> arguments.length
			<span class="hljs-keyword">when</span> <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> fn
			<span class="hljs-keyword">when</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> Function.identity
			<span class="hljs-keyword">else</span> Function.pipe [arguments...].reverse()...</pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <h2 id="aspect-like-methods">Aspect-like methods</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">pre</span> = <span class="hljs-params">(pre)</span> -&gt;</span>
		<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> PreconditionError <span class="hljs-keyword">unless</span> <span class="hljs-keyword">typeof</span> pre == <span class="hljs-string">'function'</span>
		pre.but <span class="hljs-keyword">this</span>
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">post</span> = <span class="hljs-params">(post)</span> -&gt;</span>
		<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> PreconditionError <span class="hljs-keyword">unless</span> <span class="hljs-keyword">typeof</span> post == <span class="hljs-string">'function'</span>
		fn = <span class="hljs-keyword">this</span>
		<span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span>
			ret = fn.apply <span class="hljs-keyword">this</span>, args
			post.call <span class="hljs-keyword">this</span>, ret, args...
			ret</pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <h2 id="preconditions-and-postconditions">Preconditions and postconditions</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">matches</span> = -&gt;</span> <span class="hljs-literal">true</span>
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">require</span> = <span class="hljs-params">(predicate,message=<span class="hljs-string">''</span>)</span> -&gt;</span>
		<span class="hljs-property">@pre</span> <span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span>
			<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> PreconditionError(message) <span class="hljs-keyword">unless</span> predicate.apply <span class="hljs-keyword">this</span>, args
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">ensure</span> = <span class="hljs-params">(predicate,message=<span class="hljs-string">''</span>)</span> -&gt;</span>
		<span class="hljs-property">@post</span> <span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span>
			<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> PostconditionError(message) <span class="hljs-keyword">unless</span> predicate.apply <span class="hljs-keyword">this</span>, args</pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <h2 id="typed-functions">Typed functions</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	Function.<span class="hljs-function"><span class="hljs-title">hastypes</span> = <span class="hljs-params">(types...)</span> -&gt;</span>
		predicate = Array.hastypes types...
		<span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span> predicate args
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">Requiring</span> = <span class="hljs-params">(predicate,message)</span> -&gt;</span>
		<span class="hljs-property">@then</span> <span class="hljs-function"><span class="hljs-params">(fn)</span> -&gt;</span>
			fn2 = fn.<span class="hljs-built_in">require</span> predicate, message
			fn2.matches = predicate
			<span class="hljs-keyword">return</span> fn2
	
	Function.<span class="hljs-function"><span class="hljs-title">Requiring</span> = <span class="hljs-params">(predicate,message)</span> -&gt;</span>
		<span class="hljs-function"><span class="hljs-params">(fn)</span> -&gt;</span>
			fn2 = fn.<span class="hljs-built_in">require</span> predicate, message
			fn2.matches = predicate
			<span class="hljs-keyword">return</span> fn2
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">Ensuring</span> = <span class="hljs-params">(predicate,message)</span> -&gt;</span>
		<span class="hljs-property">@then</span> <span class="hljs-function"><span class="hljs-params">(fn)</span> -&gt;</span> fn.ensure predicate, message
	
	Function.<span class="hljs-function"><span class="hljs-title">Ensuring</span> = <span class="hljs-params">(predicate,message)</span> -&gt;</span>
		<span class="hljs-function"><span class="hljs-params">(fn)</span> -&gt;</span> fn.ensure predicate, message
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">From</span> = <span class="hljs-params">(types...)</span> -&gt;</span>
		<span class="hljs-property">@Requiring</span> Function.hastypes(types...), <span class="hljs-string">'Incorrect source type. Arguments are'</span>
	
	Function.<span class="hljs-function"><span class="hljs-title">From</span> = <span class="hljs-params">(types...)</span> -&gt;</span>
		Function.Requiring Function.hastypes(types...), <span class="hljs-string">'Incorrect source type. Arguments are'</span>
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">To</span> = <span class="hljs-params">(type)</span> -&gt;</span>
		<span class="hljs-property">@Ensuring</span> Object.isa(type), <span class="hljs-string">'Incorrect target type. Returned value is'</span>
	
	Function.<span class="hljs-function"><span class="hljs-title">To</span> = <span class="hljs-params">(type)</span> -&gt;</span>
		Function.Ensuring Object.isa(type), <span class="hljs-string">'Incorrect target type. Returned value is'</span>
	
	Function.<span class="hljs-function"><span class="hljs-title">Of</span> = <span class="hljs-params">(constructor)</span> -&gt;</span>
		ensure = Object.ensure constructor
		<span class="hljs-function"><span class="hljs-params">(fn)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span> fn.call <span class="hljs-keyword">this</span>, ensure args...
	
	<span class="hljs-built_in">window</span>.Predicate = Function.To Boolean</pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <h2 id="argument-manipulation">Argument manipulation</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	Function.<span class="hljs-function"><span class="hljs-title">Defaults</span> = <span class="hljs-params">(defaults={})</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(fn)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(object)</span> -&gt;</span> fn.call <span class="hljs-keyword">this</span>, Object.union(defaults,object)
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">Defaults</span> = <span class="hljs-params">(defaults={})</span> -&gt;</span> 
		fn1 = <span class="hljs-keyword">this</span>
		<span class="hljs-function"><span class="hljs-params">(fn)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(object)</span> -&gt;</span> fn1.call(<span class="hljs-keyword">this</span>,fn).call <span class="hljs-keyword">this</span>, Object.union(defaults,object)
		
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">defaults</span> = <span class="hljs-params">(defaults={})</span> -&gt;</span>
		fn = <span class="hljs-keyword">this</span>
		<span class="hljs-function"><span class="hljs-title">fn2</span> = <span class="hljs-params">(object)</span> -&gt;</span> 
			<span class="hljs-keyword">if</span> <span class="hljs-keyword">this</span>.constructor == fn2
				<span class="hljs-keyword">new</span> fn Object.union(defaults,object)
			<span class="hljs-keyword">else</span>
				fn.call <span class="hljs-keyword">this</span>, Object.union(defaults,object)</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <h2 id="return-value-manipulation">Return value manipulation</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	Function.<span class="hljs-function"><span class="hljs-title">Returning</span> = <span class="hljs-params">(val)</span> -&gt;</span>
		<span class="hljs-function"><span class="hljs-params">(fn)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span>
			ret = val.call(<span class="hljs-keyword">this</span>)
			fn.call(<span class="hljs-keyword">this</span>,ret).apply(<span class="hljs-keyword">this</span>,args)
			<span class="hljs-keyword">return</span> ret
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">Returning</span> = <span class="hljs-params">(val)</span> -&gt;</span>
		Function.Returning(val).<span class="hljs-keyword">then</span>(<span class="hljs-keyword">this</span>)

	Function.<span class="hljs-function"><span class="hljs-title">Constant</span> = <span class="hljs-params">(constant)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(fn)</span> -&gt;</span> fn.but<span class="hljs-function"> -&gt;</span> constant
	
	Function.Chaining = Function.From<span class="hljs-function"><span class="hljs-params">(Function)</span> <span class="hljs-params">(fn)</span> -&gt;</span> fn.but<span class="hljs-function"> -&gt;</span> <span class="hljs-keyword">this</span>
	
	<span class="hljs-attribute">Function</span>::Chaining = Function.From<span class="hljs-function"><span class="hljs-params">(Function)</span> <span class="hljs-params">(fn)</span> -&gt;</span> @(fn).but<span class="hljs-function"> -&gt;</span> <span class="hljs-keyword">this</span></pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <h2 id="logical-functions">Logical functions</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	<span class="hljs-attribute">Function</span>::<span class="hljs-keyword">and</span> = Function.From(Function) \
		<span class="hljs-function"><span class="hljs-params">(predicate2)</span> -&gt;</span>
			predicate1 = <span class="hljs-keyword">this</span>
			<span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span>
				Boolean.<span class="hljs-keyword">and</span> \
					predicate1.apply(<span class="hljs-keyword">this</span>,args),
					predicate2.apply(<span class="hljs-keyword">this</span>,args)
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-keyword">or</span> = Function.From(Function) \
		<span class="hljs-function"><span class="hljs-params">(predicate2)</span> -&gt;</span>
			predicate1 = <span class="hljs-keyword">this</span>
			<span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span>
				Boolean.<span class="hljs-keyword">or</span> \
					predicate1.apply(<span class="hljs-keyword">this</span>,args),
					predicate2.apply(<span class="hljs-keyword">this</span>,args)
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">not</span> = -&gt;</span>
		predicate = <span class="hljs-keyword">this</span>
		Predicate <span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span> <span class="hljs-keyword">not</span> predicate.apply <span class="hljs-keyword">this</span>, args
	
	Function.<span class="hljs-keyword">and</span> = Function.From([Function]).To(Function) \
		<span class="hljs-function"><span class="hljs-params">(predicate,predicates...)</span> -&gt;</span>
			<span class="hljs-keyword">switch</span> arguments.length
				<span class="hljs-keyword">when</span> <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> predicate
				<span class="hljs-keyword">when</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> Boolean.True
				<span class="hljs-keyword">else</span> predicate.<span class="hljs-keyword">and</span> Function.<span class="hljs-keyword">and</span> predicates...
	
	Function.<span class="hljs-keyword">or</span> = Function.From([Function]).To(Function) \
		<span class="hljs-function"><span class="hljs-params">(predicate,predicates...)</span> -&gt;</span>
			<span class="hljs-keyword">switch</span> arguments.length
				<span class="hljs-keyword">when</span> <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> predicate
				<span class="hljs-keyword">when</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> Boolean.False
				<span class="hljs-keyword">else</span> predicate.<span class="hljs-keyword">or</span> Function.<span class="hljs-keyword">or</span> predicates...
	
	Function.<span class="hljs-function"><span class="hljs-title">not</span> = <span class="hljs-params">(predicate)</span> -&gt;</span>
		<span class="hljs-keyword">if</span> <span class="hljs-keyword">typeof</span> predicate == <span class="hljs-string">'function'</span> <span class="hljs-keyword">then</span> predicate.<span class="hljs-keyword">not</span>() <span class="hljs-keyword">else</span> <span class="hljs-keyword">not</span> predicate</pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <h2 id="delegation">Delegation</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	Function.<span class="hljs-function"><span class="hljs-title">delegate</span> = <span class="hljs-params">(fn)</span> -&gt;</span>
		<span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span> 
			[context,method] = fn.call(<span class="hljs-keyword">this</span>)
			method.apply context, args
			
	Function.<span class="hljs-function"><span class="hljs-title">delegates</span> = <span class="hljs-params">(fn)</span> -&gt;</span>
		<span class="hljs-keyword">for</span> index <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span><span class="hljs-number">.9</span>]
			<span class="hljs-keyword">do</span> <span class="hljs-function"><span class="hljs-params">(index)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span>
				[context,fns...] = fn.call(<span class="hljs-keyword">this</span>)
				fns[index].apply context, args</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <h2 id="composite-ordering">Composite ordering</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	Function.ordering = Function.<span class="hljs-keyword">or</span></pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <h2 id="predicates">Predicates</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	<span class="hljs-attribute">Function</span>::<span class="hljs-keyword">is</span> = <span class="hljs-attribute">Function</span>::<span class="hljs-keyword">then</span>
	
	Function.<span class="hljs-function"><span class="hljs-title">eq</span>  = <span class="hljs-params">(value)</span> -&gt;</span> Predicate <span class="hljs-function"><span class="hljs-params">(x)</span> -&gt;</span> x == value
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">eq</span> = <span class="hljs-params">(value)</span> -&gt;</span> <span class="hljs-property">@is</span> Function.eq value
	
	Function.<span class="hljs-function"><span class="hljs-title">neq</span>  = <span class="hljs-params">(value)</span> -&gt;</span> Predicate <span class="hljs-function"><span class="hljs-params">(x)</span> -&gt;</span> x != value
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">neq</span> = <span class="hljs-params">(value)</span> -&gt;</span> <span class="hljs-property">@is</span> Function.neq value
	
	Function.<span class="hljs-function"><span class="hljs-title">lt</span>  = <span class="hljs-params">(value)</span> -&gt;</span> Predicate <span class="hljs-function"><span class="hljs-params">(x)</span> -&gt;</span> x &lt; value
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">lt</span> = <span class="hljs-params">(value)</span> -&gt;</span> <span class="hljs-property">@is</span> Function.lt value
	
	Function.<span class="hljs-function"><span class="hljs-title">gt</span>  = <span class="hljs-params">(value)</span> -&gt;</span> Predicate <span class="hljs-function"><span class="hljs-params">(x)</span> -&gt;</span> x &gt; value
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">gt</span> = <span class="hljs-params">(value)</span> -&gt;</span> <span class="hljs-property">@is</span> Function.gt value
	
	Function.<span class="hljs-function"><span class="hljs-title">lte</span>  = <span class="hljs-params">(value)</span> -&gt;</span> Predicate <span class="hljs-function"><span class="hljs-params">(x)</span> -&gt;</span> x &lt;= value
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">lte</span> = <span class="hljs-params">(value)</span> -&gt;</span> <span class="hljs-property">@is</span> Function.lte value
	
	Function.<span class="hljs-function"><span class="hljs-title">gte</span>  = <span class="hljs-params">(value)</span> -&gt;</span> Predicate <span class="hljs-function"><span class="hljs-params">(x)</span> -&gt;</span> x &gt;= value
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">gte</span> = <span class="hljs-params">(value)</span> -&gt;</span> <span class="hljs-property">@is</span> Function.gte value
	
	Function.<span class="hljs-function"><span class="hljs-title">between</span> = <span class="hljs-params">( Function.Requiring (lower,higher) -&gt; lower &lt; higher )</span> <span class="hljs-params">(lower,higher)</span> -&gt;</span>
		Predicate <span class="hljs-function"><span class="hljs-params">(x)</span> -&gt;</span> lower &lt;= x &lt;= higher
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">between</span> = <span class="hljs-params">(lower,higher)</span> -&gt;</span> <span class="hljs-property">@is</span> Function.between lower,higher
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">matches</span> = <span class="hljs-params">(regex)</span> -&gt;</span> <span class="hljs-property">@is</span> Predicate <span class="hljs-function"><span class="hljs-params">(x)</span> -&gt;</span> regex.test x
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">isnull</span> = -&gt;</span> <span class="hljs-property">@is</span> Function.eq <span class="hljs-literal">null</span>
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">hastype</span> = <span class="hljs-params">(type)</span> -&gt;</span> <span class="hljs-property">@is</span> Object.type.eq type
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">isa</span> = <span class="hljs-params">(constructor)</span> -&gt;</span> <span class="hljs-keyword">this</span> == constructor <span class="hljs-keyword">or</span> <span class="hljs-property">@prototype</span> <span class="hljs-keyword">instanceof</span> constructor</pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <h2 id="application-methods">Application methods</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">bind</span> = <span class="hljs-params">(context,args1...)</span> -&gt;</span>
		fn = <span class="hljs-keyword">this</span>
		<span class="hljs-function"><span class="hljs-params">(args2...)</span> -&gt;</span> fn.call context, args1..., args2...
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">curry</span> = <span class="hljs-params">(args1...)</span> -&gt;</span>
		fn = <span class="hljs-keyword">this</span>
		<span class="hljs-function"><span class="hljs-params">(args2...)</span> -&gt;</span> fn.call <span class="hljs-keyword">this</span>, args1..., args2...
	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">except</span> = <span class="hljs-params">(handler)</span> -&gt;</span>
		fn = <span class="hljs-keyword">this</span>
		<span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span>
			<span class="hljs-keyword">try</span> fn.apply <span class="hljs-keyword">this</span>, args
			<span class="hljs-keyword">catch</span> error
				handler.call <span class="hljs-keyword">this</span>, error
				
	Function.<span class="hljs-function"><span class="hljs-title">Cache</span> = <span class="hljs-params">(fn)</span> -&gt;</span> fn.cache()
	
	<span class="hljs-attribute">Function</span>::cache = <span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">Cache</span> = -&gt;</span>
		cache = []
		<span class="hljs-function"><span class="hljs-title">lookup</span> = <span class="hljs-params">(args)</span> -&gt;</span> cache.find<span class="hljs-function"> -&gt;</span> Array.equal <span class="hljs-property">@args</span>, args
		fn = <span class="hljs-property">@post</span> <span class="hljs-function"><span class="hljs-params">(ret,args...)</span> -&gt;</span> cache.push <span class="hljs-attribute">args</span>: args, <span class="hljs-attribute">ret</span>: ret
		<span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span> lookup(args)?.ret ? fn.apply <span class="hljs-keyword">this</span>, arguments
	
	<span class="hljs-attribute">Function</span>::delay ?= <span class="hljs-function"><span class="hljs-params">(duration=<span class="hljs-number">1</span>,args1...)</span> -&gt;</span>
		fn = <span class="hljs-keyword">this</span>
		<span class="hljs-function"><span class="hljs-params">(args2...)</span> -&gt;</span> setTimeout fn.curry.call(fn,args1...,args2...), duration</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <h2 id="mapping-methods">Mapping methods</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>		
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">map</span> = <span class="hljs-params">(mapping)</span> -&gt;</span> <span class="hljs-property">@then</span> Function.map mapping</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <h2 id="ordering-methods">Ordering methods</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">asc</span> = -&gt;</span>
		fn = <span class="hljs-keyword">this</span>
		<span class="hljs-function"><span class="hljs-params">(a,b)</span> -&gt;</span>
			fna = fn.call <span class="hljs-keyword">this</span>, a
			fnb = fn.call <span class="hljs-keyword">this</span>, b
			<span class="hljs-keyword">return</span> -<span class="hljs-number">1</span> <span class="hljs-keyword">if</span> fna &lt; fnb
			<span class="hljs-keyword">return</span>  <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> fna &gt; fnb
			<span class="hljs-keyword">return</span>  <span class="hljs-number">0</span>
			
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">desc</span> = -&gt;</span> <span class="hljs-property">@asc</span><span class="hljs-function"><span class="hljs-params">()</span>.<span class="hljs-title">then</span> <span class="hljs-params">(x)</span> -&gt;</span> -x</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <h2 id="constructor-methods">Constructor methods</h2>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">create</span> = <span class="hljs-params">(args...)</span> -&gt;</span> Object.construct(<span class="hljs-keyword">this</span>)(args...)</pre></div></div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <h2 id="restricted-types">Restricted types</h2>

            </div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>NOTE: Make this work with objects other than strings and numbers</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>	<span class="hljs-attribute">Function</span>::<span class="hljs-function"><span class="hljs-title">Where</span> = <span class="hljs-params">(predicate,message=<span class="hljs-string">'Invalid value'</span>)</span> -&gt;</span>
		restricted = <span class="hljs-property">@post</span> <span class="hljs-function"><span class="hljs-params">(value)</span> -&gt;</span> <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> PostconditionError(message.replace(<span class="hljs-string">'&lt;value&gt;'</span>,value)) <span class="hljs-keyword">unless</span> predicate.call value, value
		restricted.base = <span class="hljs-property">@base</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">this</span>
		restricted[property] = value <span class="hljs-keyword">for</span> property, value <span class="hljs-keyword">of</span> restricted.base
		restricted.<span class="hljs-function"><span class="hljs-title">valid</span> = <span class="hljs-params">(value)</span> -&gt;</span> Object.isa(restricted.base)(value) <span class="hljs-keyword">and</span> predicate.call value, value
		<span class="hljs-keyword">return</span> restricted
	
	<span class="hljs-built_in">window</span>.Constructor = Constructor = Function
		
	Constructor.<span class="hljs-function"><span class="hljs-title">Inheriting</span> = <span class="hljs-params">(parent)</span> -&gt;</span>
		<span class="hljs-function"><span class="hljs-title">fn1</span> = -&gt;</span>
		fn1.<span class="hljs-function"><span class="hljs-title">valid</span> = <span class="hljs-params">(constructor)</span> -&gt;</span> constructor.isa parent
		<span class="hljs-keyword">return</span> fn1</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
